package com.android.youwenchuang.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;

import com.android.youwenchuang.App;
import com.android.youwenchuang.base.BaseActivity;
import com.android.youwenchuang.bean.News;
import com.android.youwenchuang.databinding.ActivityAddNewsBinding;
import com.android.youwenchuang.util.Base64Util;
import com.android.youwenchuang.util.GlideEngine;
import com.android.youwenchuang.util.UserData;
import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.hjq.bar.TitleBar;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;

import java.util.ArrayList;
import java.util.Date;

/**
 * 发布新闻
 */
public class SendNewsActivity extends BaseActivity {


    private ActivityAddNewsBinding viewBinding;
    private String selectPic = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding = ActivityAddNewsBinding.inflate(LayoutInflater.from(this));
        setContentView(viewBinding.getRoot());
        initTitleBar();
        mTitleBar.getRightView().setText("发布");
        viewBinding.ivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PictureSelector.create(SendNewsActivity.this)
                        .openGallery(SelectMimeType.ofImage())
                        .setMaxSelectNum(1)
                        .setImageEngine(GlideEngine.createGlideEngine())
                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                            @Override
                            public void onResult(ArrayList<LocalMedia> result) {
                                if (!CollectionUtils.isEmpty(result)) {
                                    String path = result.get(0).getRealPath();
                                    Glide.with(SendNewsActivity.this)
                                            .load(path)
                                            .into(viewBinding.ivImage);
                                    selectPic = "data:image/png;base64," + Base64Util.encodeFileToBase64(path);
                                }
                            }

                            @Override
                            public void onCancel() {

                            }
                        });

            }
        });
    }

    @Override
    public void onRightClick(TitleBar titleBar) {
        super.onRightClick(titleBar);
        if (TextUtils.isEmpty(viewBinding.etTitle.getText())) {
            ToastUtils.showShort("请输入标题");
            return;
        }
        if (TextUtils.isEmpty(viewBinding.etType.getText())) {
            ToastUtils.showShort("请输入分类");
            return;
        }
        if (TextUtils.isEmpty(viewBinding.etText.getText())) {
            ToastUtils.showShort("请输入内容");
            return;
        }
        News news = new News();
        news.setTitle(viewBinding.etTitle.getText().toString());
        news.setType(viewBinding.etType.getText().toString());
        news.setContent(viewBinding.etText.getText().toString());
        news.setPics(selectPic);
        news.setDate(new Date());
        news.setUser(UserData.user.name);
        App.mApp.getDaoSession().getNewsDao().insert(news);
        ToastUtils.showShort("发布成功");
        finish();
    }
}

